package flc.ast.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import flc.ast.AppConfig;
import flc.ast.R;
import flc.ast.activity.AlbumActivity;
import flc.ast.activity.CaricatureDetailActivity;
import flc.ast.activity.ImgDetailActivity;
import flc.ast.activity.SearchActivity;
import flc.ast.adapter.RecommendAdapter;
import flc.ast.databinding.FragmentAlbumBinding;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;
import stark.common.bean.StkTagResBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link AlbumFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class AlbumFragment extends BaseNoModelFragment<FragmentAlbumBinding> {

    private RecommendAdapter recommendAdapter;
    private List<StkTagResBean> listType = new ArrayList<>();

    @Override
    protected int onCreate() {
        return R.layout.fragment_album;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.ivSearch.setOnClickListener(this);
        mDataBinding.ivAlbumType1.setOnClickListener(this);
        mDataBinding.ivAlbumType2.setOnClickListener(this);
        mDataBinding.ivAlbumType3.setOnClickListener(this);
        mDataBinding.ivAlbumType4.setOnClickListener(this);
        mDataBinding.tvRecommendAll.setOnClickListener(this);

        mDataBinding.rvRecommendList.setLayoutManager(new GridLayoutManager(mContext, 2));
        recommendAdapter = new RecommendAdapter();
        mDataBinding.rvRecommendList.setAdapter(recommendAdapter);
        recommendAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivSearch://搜索
                String key = mDataBinding.etSearch.getText().toString().trim();
                if (key.isEmpty()) {
                    ToastUtils.showShort(getString(R.string.please_input_text));
                    return;
                }
                SearchActivity.key = key;
                startActivity(SearchActivity.class);
                break;
            case R.id.ivAlbumType1://男生
                AlbumActivity.type = 0;
                AlbumActivity.url = AppConfig.URL_PREFIX + listType.get(0).getHashid();
                startActivity(AlbumActivity.class);
                break;
            case R.id.ivAlbumType2://女生
                AlbumActivity.type = 1;
                AlbumActivity.url = AppConfig.URL_PREFIX + listType.get(1).getHashid();
                startActivity(AlbumActivity.class);
                break;
            case R.id.ivAlbumType3://闺蜜
                AlbumActivity.type = 2;
                AlbumActivity.url = AppConfig.URL_PREFIX + listType.get(2).getHashid();
                startActivity(AlbumActivity.class);
                break;
            case R.id.ivAlbumType4://情侣
                AlbumActivity.type = 3;
                AlbumActivity.url = AppConfig.URL_PREFIX + listType.get(3).getHashid();
                startActivity(AlbumActivity.class);
                break;
            case R.id.tvRecommendAll://为你推荐
                AlbumActivity.type = 4;
                AlbumActivity.url = AppConfig.RECOMMEND_DATA;
                startActivity(AlbumActivity.class);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        StkResBean bean = (StkResBean) adapter.getItem(position);
        ImgDetailActivity.stkResBean = bean;
        startActivity(ImgDetailActivity.class);
    }

    @Override
    protected void initData() {
        getTypeData();
        getRecommendData();
    }

    private void getTypeData() {
        StkResApi.getChildTagAndResource(null, AppConfig.ALBUM_TYPE_DATA, StkResApi.createParamMap(0, 10), false, new IReqRetCallback<List<StkTagResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkTagResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        listType.addAll(result);
                    }
                }
            }
        });
    }

    private void getRecommendData() {
        StkResApi.getTagResourceList(this, AppConfig.RECOMMEND_DATA, StkResApi.createParamMap(0, 10), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        recommendAdapter.setList(result);
                    }
                }
            }
        });
    }

}